import { updateStorage } from './tools'
import request from '../utils/request'
let userInfo = localStorage.getItem('userInfo')
try {
    userInfo = JSON.parse(userInfo) || {}
} catch (err) {
    userInfo = {}
}

export default {
    // 启用命名空间
    namespaced: true,
    state: {
        userInfo,
    },
    getters: {
        //判断是否登录 返回token值
        isLogin(state) {
            return !!state.userInfo.token
        }

    },
    mutations: {
        //登录 用户信息存入本地存储
        login(state, payload) {
            state.userInfo = payload
            updateStorage('userInfo', payload)
        },
        //登出 清除本地存储
        logout(state) {
            state.userInfo = {}
            localStorage.removeItem('userInfo')
        }

    },
    actions: {
        async login(context,payload){
            const { data } = await request.post('/managelogin',payload)
            context.commit('login',data)
            return data
        },
        async regist(context,payload){
            const {data}=await request.post('/reg',payload)
            return data
        }

    }
}